//https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/
  class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
  class Solution {
      int index;
      public TreeNode buildTree(int[] inorder, int[] postorder) {
          index = inorder.length-1;
          TreeNode root = buildTreeSub(postorder,inorder,0,postorder.length-1);
          return root;
      }


      private TreeNode buildTreeSub(int[] postorder, int[] inorder, int start, int end) {
          if (end < start) return null;
          int center = findCenter(postorder[index--],inorder);
          TreeNode root = new TreeNode(inorder[center]);
          root.right = buildTreeSub(postorder, inorder, center+1, end);
          root.left = buildTreeSub(postorder, inorder, start, center-1);
          return root;
      }

      private int findCenter(int centerVal, int[] inorder) {
          for (int i = 0; i < inorder.length; i++) {
              if (inorder[i] == centerVal) return i;
          }
          return -1;
      }
  }